<?php
function pageload(){
	fixnotexist();
	
	$getlang = _GET("lang");
	//$seslang = _SESSION("lang");
	$seslang = NULL;
	if(is_null($getlang)){
		if(is_null($seslang)){
			$lang = explode(',',checknull("HTTP_ACCEPT_LANGUAGE",$_SERVER));
		}else{
	    $lang[0] = $seslang;
		}
	}else{
		$lang = explode(',', $getlang);
	}
	$getq = _GET("q"," ");
	$_GET["q"]=filtration($getq);
	$lang[0] = preg_replace("/zh-(tw|cn)/i", "$1", strtolower(checknull("0", $lang)));
	return $lang[0];
}
function filtration($input){
//禁止ワード
  $notallowedWord = array(
      "\"",
      "\\",
  );
  $result = str_replace(  $notallowedWord ,NULL,$input);
  return is_null($result)?" ":$result;
}
//fix PHP Notice $_GET
function _GET($str,$defaultval=NULL){
    $val = checknull($str,$_GET,$defaultval);
    return $val;
}
//fix PHP Notice $_POST
function _POST($str,$defaultval=NULL){
    $val = checknull($str,$_POST,$defaultval);
    return $val;
}
//fix PHP Notice $_SESSION
function _SESSION($str,$defaultval=NULL){
    $val = checknull($str,$_SESSION,$defaultval);
    return $val;
}
/*
 * $key the key in zhe array
 * $array array
 * $result default value
 * return ($result)the key in zhe array or default
 */
function checknull($key,$array,$result=NULL){
    if(is_array($array)){
        if(array_key_exists($key,$array)){ 
            if(is_array($array[$key])){ 
                $result=$array[$key]; 
            }elseif(strlen($array[$key])){ 
                $result=$array[$key]; 
            } 
        }
    }
    return $result;
}

function fixnotexist(){
	$jump_to_file = _GET("jump");//.htaccessからもらいます
	if($jump_to_file!=NULL){//ファイルがない場合
		/*
	    $jump_to_file = preg_replace("/\/{1,}|\\{1,}/i",DIRECTORY_SEPARATOR, $jump_to_file);
	    if(file_exists($jump_to_file)){
	        $jump_to_file_content = @file_get_contents( $jump_to_file );
	        echo $jump_to_file_content;
	    }
	    
	    //else{
	    //    echo $jump_to_file;
	    //}
	    */
	    exit;
	}
}
	/**
   * Amazon
	 * Get signature.
	 * @param $arrquery array querystring array
	 * @return string signature
	 */
function GetSignature($transform,$host,$uri,$SECRET_KEY,$arrquery){
		$string_to_sign = "GET\n".$host."\n".$uri."\n".$transform->GetQuery($arrquery);
		$signature = base64_encode(hash_hmac("sha256", $string_to_sign,$SECRET_KEY, True));
		$signature = str_replace("%7E", "~", rawurlencode($signature));
		return $signature;
}
/*
define('IS_ROBOT', is_bot());
//SEO
function is_bot($useragent = '')
{
	static $char_spiders  = "/(bot|crawl|Spider|slurp|yahoo|sohu-search|lycos|robozilla)/i";
	static $char_browsers = "/(msie|netscape|opera|konqueror|mozilla)/i";
	
	$useragent = strtolower(empty($useragent) ? checknull("HTTP_USER_AGENT",$_SERVER) : $useragent);
	if(strpos($useragent, 'http://') === false && preg_match($char_browsers, $useragent)) return false;
	if(preg_match($char_spiders, $useragent)) return true;
	
	return false;

}
function checkmobile() {
	global $_G;
	$mobile = array();
	static $mobilebrowser_list =array('iphone', 'android', 'phone', 'mobile', 'wap', 'netfront', 'java', 'opera mobi', 'opera mini',
				'ucweb', 'windows ce', 'symbian', 'series', 'webos', 'sony', 'blackberry', 'dopod', 'nokia', 'samsung',
				'palmsource', 'xda', 'pieplus', 'meizu', 'midp', 'cldc', 'motorola', 'foma', 'docomo', 'up.browser',
				'up.link', 'blazer', 'helio', 'hosin', 'huawei', 'novarra', 'coolpad', 'webos', 'techfaith', 'palmsource',
				'alcatel', 'amoi', 'ktouch', 'nexian', 'ericsson', 'philips', 'sagem', 'wellcom', 'bunjalloo', 'maui', 'smartphone',
				'iemobile', 'spice', 'bird', 'zte-', 'longcos', 'pantech', 'gionee', 'portalmmm', 'jig browser', 'hiptop',
				'benq', 'haier', '^lct', '320x320', '240x320', '176x220');
	$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
	if(($v = dstrpos($useragent, $mobilebrowser_list, true))) {
		$_G['mobile'] = $v;
		return true;
	}
	$brower = array('mozilla', 'chrome', 'safari', 'opera', 'm3gate', 'winwap', 'openwave', 'myop');
	if(dstrpos($useragent, $brower)) return false;

	$_G['mobile'] = 'unknown';
	if($_GET['mobile'] === 'yes') {
		return true;
	} else {
		return false;
	}
}
function dstrpos($string, &$arr, $returnvalue = false) {
	if(empty($string)) return false;
	foreach((array)$arr as $v) {
		if(strpos($string, $v) !== false) {
			$return = $returnvalue ? $v : true;
			return $return;
		}
	}
	return false;
}
*/